package com.zero.insp.services;

import com.github.pagehelper.PageHelper;
import com.zero.insp.common.PPageInfo;
import com.zero.insp.dao.TUserInfoDao;
import com.zero.insp.domain.TUserInfo;
import com.zero.insp.domain.TUserInfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private TUserInfoDao userInfoDao;

    public PPageInfo<TUserInfo> query(TUserInfoExample example, int page, int size){

        PageHelper.startPage(page,size);

        List<TUserInfo> users = userInfoDao.selectByExample(example);

        PPageInfo<TUserInfo> result = new PPageInfo<>(users);

        return result;
    }

    public int add(TUserInfo userInfo){
        return userInfoDao.insert(userInfo);
    }

    public int delete(long user_id){
        return userInfoDao.deleteByPrimaryKey(user_id);
    }
    public int edit(TUserInfo userInfo, long user_id){
        userInfo.setId(user_id);
        return userInfoDao.updateByPrimaryKeySelective(userInfo);
    }

    public TUserInfo findUserByAccount(String user_account){
        TUserInfoExample example = new TUserInfoExample();
        example.createCriteria().andAccountEqualTo(user_account);

        List<TUserInfo> users = userInfoDao.selectByExample(example);

        if (!CollectionUtils.isEmpty(users)){
            return users.get(0);
        }
        return null;
    }
    public TUserInfo findUserById(long user_id){

        return userInfoDao.selectByPrimaryKey(user_id);
    }
    public boolean exist(String user_account){
        TUserInfoExample example = new TUserInfoExample();
        example.createCriteria().andAccountEqualTo(user_account);

        long count = userInfoDao.countByExample(example);

        return count>0?true:false;
    }
    public boolean existName(String user_name){
        TUserInfoExample example = new TUserInfoExample();
        example.createCriteria().andNameEqualTo(user_name);

        long count = userInfoDao.countByExample(example);

        return count>0?true:false;
    }
}